package com.zzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzx.common.domain.CommonResult;
import com.zzx.mapper.OrderMapper;
import com.zzx.pojo.UserOrder;
import com.zzx.service.IOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/10/3 18:17
 */
@Service
public class OrderServiceImpl implements IOrderService
{
    @Resource
    OrderMapper orderMapper;

    @Override
    public CommonResult getOnesOrderList(Integer userId, String orderState)
    {
        LambdaQueryWrapper<UserOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserOrder::getUserId, userId);
        wrapper.eq(null != orderState, UserOrder::getOrderState, orderState);
        List<UserOrder> userOrders = orderMapper.selectList(wrapper);
        // 更新过期订单的状态，创建时间和当前时间相差超过5分钟就算过期
        for (UserOrder userOrder : userOrders)
        {
            if ("0".equals(userOrder.getOrderState()))
            {
                // 计算createTime与当前时间相差的毫秒数
                long diff = (new Date().getTime() - userOrder.getCreateTime().getTime());
                if (diff >= 5 * 60 * 1000)
                {
                    userOrder.setOrderState("2");
                    cancelOrder(userOrder);
                }
            }
        }

        return CommonResult.ok().data("userOrders", userOrders);
    }

    @Override
    public CommonResult getOrderById(Integer orderId)
    {
        UserOrder userOrder = orderMapper.selectById(orderId);
        // 计算createTime与当前时间相差的毫秒数
        long diff = (new Date().getTime() - userOrder.getCreateTime().getTime());
        switch (userOrder.getOrderState())
        {
            case "0":
                if (diff >= 5 * 60 * 1000)
                {
                    userOrder.setRemainingTime(0L);
                    userOrder.setOrderState("2");
                    cancelOrder(userOrder);
                } else
                {
                    userOrder.setRemainingTime(5 * 60 * 1000 - diff);
                }
                break;

            case "1":
                userOrder.setRemainingTime(0L);
                break;

            case "2":
                userOrder.setRemainingTime(0L);
                break;
            default:
                break;
        }

        return CommonResult.ok().data("order", userOrder);
    }

    @Override
    public CommonResult makeOrder(UserOrder userOrder)
    {
        userOrder.setOrderState("0");
        int insert = orderMapper.insert(userOrder);

        return CommonResult.ok().data("insert", insert);
    }

    @Override
    public CommonResult cancelOrder(UserOrder userOrder)
    {
        userOrder.setOrderState("2");
        userOrder.setUpdateTime(new Date());
        int update = orderMapper.updateById(userOrder);

        return CommonResult.ok().data("update", update);
    }

    @Override
    public CommonResult payOrder(UserOrder userOrder)
    {
        userOrder.setOrderState("1");
        userOrder.setUpdateTime(new Date());
        int update = orderMapper.updateById(userOrder);

        return CommonResult.ok().data("update", update);
    }

    @Override
    public CommonResult getOnesExistOrder(Integer userId, Integer goodsId)
    {
        LambdaQueryWrapper<UserOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserOrder::getUserId, userId);
        wrapper.eq(UserOrder::getGoodsId, goodsId);
        wrapper.eq(UserOrder::getOrderState, "0"); //未处理的订单
        List<UserOrder> userOrders = orderMapper.selectList(wrapper);
        for (UserOrder userOrder : userOrders)
        {
            // 计算createTime与当前时间相差的毫秒数
            long diff = (new Date().getTime() - userOrder.getCreateTime().getTime());
            if (diff >= 5 * 60 * 1000)
            {
                cancelOrder(userOrder);
                userOrders.remove(userOrder);
            }
        }

        return CommonResult.ok().data("userOrders", userOrders);
    }
}